
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dw2.vista;


import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.desarrollosw2.Autor;
import mx.its.dsw2.biblioteca.bl.AutorBlLocal;

/**
 *
 * @author sistemas
 */
@ManagedBean
@SessionScoped
public class AuthorBean {
    //--------------------llamadas a los ejb's-----------
 @EJB

//-----------------------variables propiedades------------
private AutorBlLocal autorBl;
 private List<Autor> lista;
private Autor author;
private String accion;
    /**
     * Creates a new instance of NewJSFManagedBean
     */
    public AuthorBean() {
        author=new Autor();
    }
    //----------------metodos GET SET--------------------
public List<Autor> getLista(){
           lista=autorBl.getAll();
           return lista;
}
public Autor getAuthor() {
        return author;
    }
public void setAuthor(Autor author) {
        this.author = author;
    }
//-----------------acciones-----------------------------
public String procesarAccion(){
    if(accion.equalsIgnoreCase("Nuevo")){
        autorBl.Register(author);
        
    }else{
        if(accion.equalsIgnoreCase("Editar")){
            autorBl.modify(author);
        }else{
            if(accion.equalsIgnoreCase("Eliminar")){
                autorBl.delete(author);
            }
        }
    }
   accion="nada";
   return "AutorLista";
}
    
//public String prepararNuevo(){
//  author=new Autor();            //automaticamente se limpian todos los registros se ejecutara despues
//    return "AutorCrearEditar";
//}
//----------------Listeners----------------------------
public void prepararNuevo(ActionEvent evt){ //siempre se va ejecutar antes
    accion="Nuevo";
    author=new Autor();
//    lista.add(author);
}
public void PrepararEditar(ActionEvent evt){
    accion="Editar";
    System.out.println(evt.getComponent().getClientId());
    int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    author=lista.get(index);   

}
public void PrepararEliminar(ActionEvent evt){
    accion ="Eliminar";
    System.out.println(evt.getComponent().getClientId());
    int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    author=lista.get(index);

}
}



